"ui";
/*
 * @Author: 白酒煮饭 
 * @Date: 2019-03-07 15:48:35 
 * @Last Modified by: QQ1641763174
 * @Last Modified time: 2019-03-07 19:26:19
 */

ui.layout(
    <frame>
        <vertical>
            <horizontal>
                <img id="exit" src="file://./res/ic_keyboard_backspace_black_48dp.png" w="40" h="40" gravity="center"/>
                <text text="QQ名片赞设置"  textColor="#333333" textSize="27sp" typeface="serif" gravity="top|center" w="*" textStyle="bold"/>
            </horizontal>
            <Switch id="autoService" text="无障碍服务" checked="{{auto.service != null}}" padding="8 8 8 8" textSize="15sp" textSize="18sp"/>
            <horizontal margin="5 0 5 10">
                <text text="回赞次数:" size="70px"/>
                <radiogroup w="*" marginLeft="60px"id="choices" orientation="horizontal">
                    <radio text="10"checked="true"size="50px"/>
                    <radio text="20"checked="false"size="50px"/>
                </radiogroup>
            </horizontal>
            <horizontal w="*" bg="#d3d3d3" margin="5 10 5 10">
                <text text="自动取消金豆" size="70px"/>
                <Switch id="id_1" checked="false" w="*" gravity="right|center"/>
            </horizontal>
            <horizontal w="*" bg="#d3d3d3" margin="5 0 5 10">
                <text text="只赞好友" size="70px"/>
                <Switch id="id_2" checked="false" w="*" gravity="right|center"/>
            </horizontal>
            <text text="使用说明:"/>
            <text text="    回赞次数一般默认10就可以，超级会员可以选择20。取消金豆，如果在点赞过程中，一直出现金豆不足的情况下，可以开启。只赞好友，不需要要金豆。" margin="10 0 10 0"/>
        </vertical>
        <button text="开始运行" layout_gravity="bottom" id="btn_start" style="Widget.AppCompat.Button.Colored"w="*"h="180px"/>
    </frame>
);
var mt, id_1, id_2, num;

ui.exit.on("click",()=>{ui.finish()});
ui.autoService.on("check", function(checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if(checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if(!checked && auto.service != null){
        auto.service.disableSelf();
    }
});

// 当用户回到本界面时，resume事件会被触发
ui.emitter.on("resume", function() {
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.autoService.checked = auto.service != null;
});
ui.id_1.on("click", (a) => {
    if (a.isChecked()) {
        id_1 = 1;
    } else {
        id_1 = 0;
    }
});

ui.id_2.on("click", (b) => {
    if (b.isChecked()) {
        id_2 = 1;
    } else {
        id_2 = 0;
    }
});

ui.btn_start.on("click", function() {
    if(auto.service == null) {
        toast("请先开启无障碍服务！");
        return;
    }
    start();
});

function open() {
    num = (selectedIndex(ui.choices) == 0) ? 10 : 20;
    if(id_1) 取消金豆();
    if(id_2) {main2();}else{main();}
};

function selectedIndex(rg) {  
    let id = rg.getCheckedRadioButtonId();  
    for (let i = 0; i < rg.getChildCount(); i++) {    
        if (id == rg.getChildAt(i).getId()) {      
            return i;    
        }  
    }  
    return -1;
}


function 下滑() {
    className("AbsListView").scrollable().scrollForward();
}

function 赞() {
    className("ImageView").desc("赞").click();
}

function 显示更多() {
    for (let i = 0; i < 2; i++) {
        click("显示更多");
    }
}

function 取消金豆() {
    threads.start(function(){
    while (true) {
        if (currentActivity() == "com.tencent.mobileqq.activity.VisitorsActivity") {
            click("取消");
            sleep(300);
        }
    }
    });
}

function main() {
    threads.start(function(){
    toast("请打开自己的资料页，点击点赞图标");
    launchApp("QQ");
    while (notStopped()) {
        for (let i = 0; i < num; i++) {
            赞();
        }
        显示更多();
        下滑();
    }
    });
}

function main2() {
    threads.start(function(){
    launchApp("QQ");
    while (notStopped()) {
        var list = className("AbsListView").findOne();
        list.children().each(function(child) {
            if (!child)
                return;
            var l = child.findByText("(好友)");
            if (l.size() > 0) {
                var like = child.findOne(className("ImageView").desc("赞"));
                for (let i = 0; i < num; i++) {
                    like.click();
                    sleep(200);
                }
            }
        });
        显示更多();
        if (currentActivity() == "com.tencent.mobileqq.activity.VisitorsActivity") {
            list.scrollForward();
        }
    }
    });
}

function stop() {
    if (mt) {
        mt.interrupt();
        threads.shutDownAll();
        setTimeout(function() {
            if (mt.isAlive()) {
                toast("停止失败");
                return;
            }
            toast("已停止");
            mt = null;
            ui.run(function() {
                ui.btn_start.text("启动");
            });
        }, 300);
    }
}

function start() {
    if (mt) {
        stop();
        return;
    }
    mt = threads.start(open);
    ui.run(function() {
        ui.btn_start.text("停止");
    });
}